var app = getApp();
var util = require('../../../utils/util');
var formatLocation = util.formatLocation

Page({

  /**
   * 页面的初始数据
   */
  data: {
    imageList: [],
    poolList: [],
    fishfield: {}, //
    countIndex: 0,
    isAddNew: false,
    service_flag: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    if (!app.globalData.userInfo) {
      wx.showModal({
        title: '哪有鱼提示',
        content: '没有登录信息哦',
        showCancel: false,
        success: function(res) {
          wx.navigateBack();
        }
      })
    } else {
      app.checktoken(function() {
        that.getMyList();
      });
    }
  },
  chooseLocation: function() {
    var that = this
    var _fishfield = that.data.fishfield;

    wx.chooseLocation({
      success: function(res) {
        console.log(res)
        _fishfield.fields.address = res.address;
        _fishfield.fields.longitude = res.longitude;
        _fishfield.fields.latitude = res.latitude;
        that.setData({
          fishfield: _fishfield
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  getMyList: function() {
    var that = this;
    var _token = app.globalData.token;
    wx.request({
      url: app.globalData.Domain + '/field/mylist',
      data: {
        token: _token
      },
      success: function(res) {
        if (res.data.code == 0) {
          var list = res.data.data;
          if (list.length > 0) {
            var fid = list[0].id;
            that.getDetail(fid);
          } else {
            that.setData({
              isAddNew: true
            });
          }
        } else {
          wx.showModal({
            title: '哪有鱼提示',
            content: res.data.msg,
            showCancel: false
          })
        }
      }
    })
  },
  getDetail: function(id) {
    wx.showLoading({
      title: '正在加载信息',
    })
    var that = this;
    wx.request({
      url: app.globalData.Domain + '/field/detail',
      data: {
        id: id
      },
      success: function(res) {
        var imgs = [];

        if (res.data.code == 0) {
          var albums = res.data.data.albums;
          for (var i = 0; i < albums.length; i++) {
            imgs.push(app.globalData.SiteDomain + albums[i].original_path);
          }
          that.setData({
            fishfield: res.data.data,
            imageList: imgs
          });
          that.getPool(id);
        } else {
          wx.showModal({
            title: '哪有鱼提示',
            content: res.data.msg,
            showCancel: false
          })
        }
      },
      complete: function() {
        wx.hideLoading()
      }
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  openLocation: function(e) {
    console.log(e)
    var value = e.detail.value
    console.log(value)
    wx.openLocation({
      longitude: Number(value.longitude),
      latitude: Number(value.latitude),
      name: value.name,
      address: value.address
    })
  },
  countChange: function(e) {
    this.setData({
      countIndex: e.detail.value
    })
  },
  deleteImg: function(e) {
    var that = this
    var index = e.target.dataset.index
    var tempList = that.data.imageList;
    tempList.splice(index, 1);
    that.setData({
      imageList: tempList
    })
  },
  chooseImage: function() {
    var that = this
    wx.chooseImage({
      count: 1,
      success: function(res) {
        console.log(res)
        var tempList = that.data.imageList;
        tempList.push(res.tempFilePaths[0]);
        that.setData({
          imageList: tempList
        })
        console.log(tempList);
      }
    })
  },
  previewImage: function(e) {
    var current = e.target.dataset.src
    wx.previewImage({
      current: current,
      urls: this.data.imageList
    })
  },
  formSubmit: function(e) {
    var that = this;
    var _token = app.globalData.token;
    var _model = that.data.fishfield || {
      fields: {},
      albums: []
    };
    //debugger;
    console.log(e.detail.value);
    //_model.fields.user_id=
    _model.title = e.detail.value.title;
    _model.fields.manager_name = e.detail.value.manager_name;
    _model.fields.mobile1 = e.detail.value.mobile1;
    _model.fields.mobile2 = e.detail.value.mobile2;
    _model.fields.address = e.detail.value.address;
    _model.fields.measure = e.detail.value.measure;
    _model.fields.place_count = e.detail.value.place_count;
    let _service_flag = (e.detail.value.wifi ? "WIFI，" : "") + (e.detail.value.food ? "餐饮，" : "") + (e.detail.value.camare ? "监控，" : "") + (e.detail.value.park ? "停车场" : "")
    _model.fields.service_flag = _service_flag;
    _model.fields.video_src = e.detail.value.video_src;
    wx.request({
      method: 'POST',
      url: app.globalData.Domain + '/field/save', //接口地址
      data: {
        token: _token,
        model: _model
      },
      header: {
        'content-type': 'application/json'
      },
      success: function(res) {
        if (res.data.code == 0) {
          wx.showToast({
            title: '信息修改成功',
            duration: 2000
          })
          setTimeout(function() {
            wx.navigateBack();
          }, 2000)
        } else {
          wx.showModal({
            title: '哪有鱼提示',
            content: res.data.msg
          })
        }
      },
      fail: function(res) {
        console.log('cuowu' + ':' + res)
      }
    })
  },
  getPool: function(f_id) {
    var that = this;
    var _token = app.globalData.token;
    wx.request({
      method: 'GET',
      url: app.globalData.Domain + '/pool/list', //接口地址
      data: {
        token: _token,
        field_id: f_id
      },
      header: {
        'content-type': 'application/json'
      },
      success: function(res) {
        if (res.data.code == 0) {
          that.setData({
            poolList: res.data.data
          });
        } else {
          wx.showModal({
            title: '哪有鱼提示',
            content: res.data.msg
          })
        }
      },
      fail: function(res) {
        console.log('cuowu' + ':' + res)
      }
    })
  },
  addpool: function(e) {
    var that = this;

    var _field_title = that.data.fishfield.title;
    var _field_id = that.data.fishfield.id;
    wx.navigateTo({
      url: "pool?f_id=" + _field_id + "&f_title=" + _field_title
    })
  }
})